package com.liufei1988.spring.bean.definition;

import com.liufei1988.spring.overview.domain.User;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.GenericBeanDefinition;

public class BeanDefinitionCreationDemo {
    public static void main(String[] args) {
        //1. 通过BeanDefinitionBuilder构建
        BeanDefinitionBuilder beanDefinitionBuilder=BeanDefinitionBuilder.genericBeanDefinition(User.class);
        //通过属性设置
        beanDefinitionBuilder.addPropertyValue("id",1)
                             .addPropertyValue("name","zhangsan");

        //获取BeanDefinition对象
        BeanDefinition beanDefinition=beanDefinitionBuilder.getBeanDefinition();
        //BeanDefinition并非Bean终态，可以自定义修改
        System.out.print(beanDefinition);

        //2. 通过AbstractBeanDefinition以及派生类
        GenericBeanDefinition genericBeanDefinition=new GenericBeanDefinition();
        //设置Bean类型
        genericBeanDefinition.setBeanClass(User.class);
        //通过MutablePropertyValues批量操作属性
        MutablePropertyValues propertyValues=new MutablePropertyValues();
        propertyValues.add("id",1)
                      .add("name","王四");
        //通过set MutablePropertyValues批量操作属性
        genericBeanDefinition.setPropertyValues(propertyValues);


    }
}
